{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "satisfactory-watson",
   "metadata": {},
   "source": [
    "## 五、信息论与模糊数学编程（本大题10分）"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "french-hello",
   "metadata": {},
   "source": [
    "7、以中心[(-10, -18), (0, 20), (15, -12)]和方差[0.5, 0.7, 0.1]生成聚类测试数据。然后实现模糊聚类，分别计算聚类为3类、4类的聚类中心值及fpc值。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "white-struggle",
   "metadata": {},
   "source": [
    "### 程序源代码："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "compound-brook",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "K=3时,聚类中心:\n",
      "[[ -0.08981425  19.90409635]\n",
      " [-10.02647245 -17.96743285]\n",
      " [ 14.99854582 -12.00034423]]\n",
      "K=3时,FPC指数: 1.0\n",
      "K=4时,聚类中心:\n",
      "[[-10.02647245 -17.96743285]\n",
      " [ -0.50221649  19.51863004]\n",
      " [ 14.99854582 -12.00034423]\n",
      " [  0.38074728  20.34392329]]\n",
      "K=4时,FPC指数: 0.9122204887806159\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "from sklearn.datasets import make_blobs\n",
    "from sklearn.cluster import KMeans\n",
    "from sklearn import metrics\n",
    "\n",
    "# 生成测试数据\n",
    "centers = [(-10, -18), (0, 20), (15, -12)]\n",
    "cluster_std = [0.5, 0.7, 0.1]  \n",
    "X, y = make_blobs(n_samples=500, centers=centers, cluster_std=cluster_std, random_state=0)\n",
    "\n",
    "# 模糊聚类(K=3)\n",
    "y_pred_3 = KMeans(n_clusters=3, random_state=9).fit_predict(X)\n",
    "print(\"K=3时,聚类中心:\")\n",
    "print(KMeans(n_clusters=3, random_state=9).fit(X).cluster_centers_)  \n",
    "print(\"K=3时,FPC指数:\", metrics.fowlkes_mallows_score(y, y_pred_3))\n",
    "\n",
    "# 模糊聚类(K=4)  \n",
    "y_pred_4 = KMeans(n_clusters=4, random_state=9).fit_predict(X)\n",
    "print(\"K=4时,聚类中心:\") \n",
    "print(KMeans(n_clusters=4, random_state=9).fit(X).cluster_centers_)\n",
    "print(\"K=4时,FPC指数:\", metrics.fowlkes_mallows_score(y, y_pred_4))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "colored-casting",
   "metadata": {},
   "source": [
    "### 结果分析："
   ]
  },
  {
   "cell_type": "markdown",
   "id": "french-navigation",
   "metadata": {},
   "source": [
    "1. **K=3时的聚类结果**：\n",
    "   - 输出了K=3时的聚类中心坐标，这些坐标代表了每个聚类的中心点位置。\n",
    "   - 计算并打印了K=3时的FPC指数，这个指数衡量了实际标签（y）与K=3时聚类结果（y_pred_3）之间的相似度。\n",
    "\n",
    "2. **K=4时的聚类结果**：\n",
    "   - 输出了K=4时的聚类中心坐标，这些坐标代表了每个聚类的中心点位置。\n",
    "   - 计算并打印了K=4时的FPC指数，同样衡量了实际标签（y）与K=4时聚类结果（y_pred_4）之间的相似度。\n",
    "\n",
    "3. **FPC指数的解读**：\n",
    "   - FPC指数越接近1，表示聚类结果与实际标签越相似，聚类效果越好。\n",
    "   - 当FPC指数越低，表示聚类效果可能不够理想，聚类结果与真实标签差异较大。\n",
    "\n",
    "4. **聚类中心的意义**：\n",
    "   - 聚类中心坐标是每个聚类的中心点位置，可以帮助理解聚类的分布情况。\n",
    "\n",
    "5. **结果分析**：\n",
    "   - 通过FPC指数和聚类中心，可以评估K均值聚类在不同聚类数量下的表现。\n",
    "   - K=3和K=4的情况下，可以对比不同聚类数量对聚类效果的影响。\n",
    "\n",
    "这些结果分析有助于了解K均值聚类在不同K值情况下的表现，以及在实际应用中如何选择最佳的聚类数量。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c29305d7-4de3-4d9d-9ff9-c3f82f8edcc4",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
